﻿
    using System;
namespace YoucaiReader.Helper.Drawing
{
    public static class UnicodeWestern
    {
        public const sbyte HCC_Blank = 4;
        public const sbyte HCC_Consonant = 1;
        public const sbyte HCC_Punctuation = 3;
        public const sbyte HCC_Unknown = 0;
        public const sbyte HCC_Vowel = 2;
        private static readonly sbyte[] UnicodeDescription = new sbyte[] { 
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
            4, 3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 0, 3, 3, 3, 3, 
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 3, 
            3, 2, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 
            1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 3, 3, 3, 0, 3, 
            0, 2, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 
            1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 3, 0, 3, 0, 0, 
            0, 0, 3, 1, 3, 3, 3, 3, 0, 3, 1, 3, 2, 0, 1, 0, 
            0, 3, 3, 3, 3, 3, 3, 3, 0, 0, 1, 3, 2, 0, 1, 2, 
            4, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 3, 0, 0, 
            0, 0, 0, 0, 0, 1, 0, 3, 0, 0, 0, 3, 0, 0, 0, 3, 
            2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 
            1, 1, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 1, 1, 
            2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 
            1, 1, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 1, 2
         };
        private static readonly short[][] UnicodeToWestern = new short[][] { 
            new short[] { 0x81, 0x81 }, new short[] { 0x8d, 0x8d }, new short[] { 0x8f, 0x8f }, new short[] { 0x90, 0x90 }, new short[] { 0x9d, 0x9d }, new short[] { 0x152, 140 }, new short[] { 0x153, 0x9c }, new short[] { 0x160, 0x8a }, new short[] { 0x161, 0x9a }, new short[] { 0x178, 0x9f }, new short[] { 0x17d, 0x8e }, new short[] { 0x17e, 0x9e }, new short[] { 0x192, 0x83 }, new short[] { 710, 0x88 }, new short[] { 0x2dc, 0x98 }, new short[] { 0x2011, 0x2d }, 
            new short[] { 0x2013, 150 }, new short[] { 0x2014, 0x97 }, new short[] { 0x2018, 0x91 }, new short[] { 0x2019, 0x92 }, new short[] { 0x201a, 130 }, new short[] { 0x201c, 0x93 }, new short[] { 0x201d, 0x94 }, new short[] { 0x201e, 0x84 }, new short[] { 0x2020, 0x86 }, new short[] { 0x2021, 0x87 }, new short[] { 0x2022, 0x95 }, new short[] { 0x2026, 0x85 }, new short[] { 0x2030, 0x89 }, new short[] { 0x2039, 0x8b }, new short[] { 0x203a, 0x9b }, new short[] { 0x20ac, 0x80 }, 
            new short[] { 0x2122, 0x99 }
         };
        private static readonly char[] WINDOWS_1252 = new char[] { 
            '€', '\x0081', '‚', 'ƒ', '„', '…', '†', '‡', 'ˆ', '‰', 'Š', '‹', 'Œ', '\x008d', 'Ž', '\x008f', 
            '\x0090', '‘', '’', '“', '”', '•', '–', '—', '˜', '™', 'š', '›', 'œ', '\x009d', 'ž', 'Ÿ'
         };

        public static int charToHex(char car)
        {
            if ((car >= 'a') && (car <= 'f'))
            {
                return ((car - 'a') + 10);
            }
            if ((car >= 'A') && (car <= 'F'))
            {
                return ((car - 'A') + 10);
            }
            if ((car >= '0') && (car <= '9'))
            {
                return (car - '0');
            }
            return -1;
        }

        public static sbyte[] convert(string unicode)
        {
            int length = unicode.Length;
            sbyte[] numArray = new sbyte[length];
            for (int i = 0; i < length; i++)
            {
                short a = (short) unicode[i];
                if (((a >= 0) && (a <= 0x7f)) || ((a >= 160) && (a <= 0xff)))
                {
                    numArray[i] = (sbyte) a;
                }
                else
                {
                    numArray[i] = find_u2s(a, UnicodeToWestern, 0x20);
                }
            }
            return numArray;
        }

        public static char[] convertToChar(sbyte[] windows1252, int begin, int size)
        {
            char[] chArray = new char[size];
            for (int i = 0; i < size; i++)
            {
                sbyte num2 = windows1252[i + begin];
                if (num2 >= -96)
                {
                    chArray[i] = (char) (num2 & 0xff);
                }
                else
                {
                    chArray[i] = WINDOWS_1252[0x80 + num2];
                }
            }
            return chArray;
        }

        public static sbyte find_u2s(short a, short[][] unicodeToSbcs, sbyte defaultChar)
        {
            int index = 0;
            bool flag = false;
            int num2 = 0;
            int num3 = unicodeToSbcs.Length - 1;
            if (a == unicodeToSbcs[num2][0])
            {
                return (sbyte) unicodeToSbcs[num2][1];
            }
            if (a == unicodeToSbcs[num3][0])
            {
                return (sbyte) unicodeToSbcs[num3][1];
            }
            while (!flag && ((num2 + 1) < num3))
            {
                index = (num2 + num3) >> 1;
                if (a == unicodeToSbcs[index][0])
                {
                    flag = true;
                }
                else
                {
                    if (a > unicodeToSbcs[index][0])
                    {
                        num2 = index;
                        continue;
                    }
                    num3 = index;
                }
            }
            if (flag)
            {
                return (sbyte) unicodeToSbcs[index][1];
            }
            return defaultChar;
        }

        public static sbyte getHyphenationClass(char a)
        {
            int index = a;
            if (index >= UnicodeDescription.Length)
            {
                return 0;
            }
            return UnicodeDescription[index];
        }

        public static char w2u(int a)
        {
            if ((a >= 0x80) && (a <= 0x9f))
            {
                return WINDOWS_1252[a - 0x80];
            }
            return (char) a;
        }
    }
}

